package Model;

import java.util.Date;

public class Movie {

    private String name;
    private Date year;
    private String director;
    private int rating;

    public Movie(String name, Date year, String director){
        this.name = name;
        this.year = year;
        this.director = director;
        this.rating = Rating.NO_RATING;
    }

    public boolean rate(int rating){
        if(Rating.isRatingValid(rating)){
            this.rating = rating;
            return true;
        }
        return false;
    }

    public String getString(){
        return String.format("%-25s", name)+ "\t|\t"
                + String.format("%-4s", year.getYear())
                +"\t|\t" + String.format("%-20s", director)
                +"\t|\t" + String.format("%-10s", Rating.ratingToStar(rating));
    }


    private static class Rating{
        private static final int MIN_RATING = 1;
        private static final int MAX_RATING = 10;
        public static final int NO_RATING = 0;


        public static boolean isRatingValid(int rating) {
            return rating >= MIN_RATING && rating <= MAX_RATING;
        }

        public static String ratingToStar(int rating) {
            String stars = "";
            if (rating != Rating.NO_RATING){
                for (int ratingIndex = 1; ratingIndex <= rating; ratingIndex++) {
                    stars += "*";
                }
            }
            else
                stars = "N/A";
            return stars;
        }
    }
}
